home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / gc / GChdr_alloc.c < prev    next >
C/C++ Source or Header  |  1991-09-11  |  5KB  |  169 lines

  1.  
  2. /* begincopyright
  3.   Copyright (c) 1988,1990 Xerox Corporation. All rights reserved.
  4.   Use and copying of this software and preparation of derivative works based
  5.   upon this software are permitted. Any distribution of this software or
  6.   derivative works must comply with all applicable United States export
  7.   control laws. This software is made available AS IS, and Xerox Corporation
  8.   makes no warranty about the software, its performance or its conformity to
  9.   any specification. Any person obtaining a copy of this software is requested
  10.   to send their name and post office or electronic mail address to:
  11.     PCR Coordinator
  12.     Xerox PARC
  13.     3333 Coyote Hill Rd.
  14.     Palo Alto, CA 94304
  15.     
  16.   Parts of this software were derived from code bearing the copyright notice:
  17.   
  18.   Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  19.   This material may be freely distributed, provided this notice is retained.
  20.   This material is provided as is, with no warranty expressed or implied.
  21.   Use at your own risk.
  22.   
  23.   endcopyright */
  24.   
  25. # define DEBUG_SMASH
  26. # include "xr/GCPrivate.h"
  27. # ifdef DIRTY_BITS
  28. #  include "sys/mman.h"
  29. # endif
  30. # define MAX_HEADER_AREAS 256
  31.  
  32. /*
  33.  * Boehm, February 22, 1991 3:12:53 pm PST
  34.  */
  35.  
  36. # ifdef SEPARATE_HEADERS
  37.  
  38. /* Should be static, but not scanned.  Must match decl in GCglobals.c */
  39. extern struct hdr_area {
  40.     char * ha_start;
  41.     char * ha_end;
  42. } GC_header_areas[];
  43. # define header_areas GC_header_areas
  44.  
  45. int GC_num_ha = 0;
  46. # define num_ha GC_num_ha
  47.  
  48. # ifdef DEBUG_SMASH
  49.     hdr * GC_last_allocated_hdr;
  50.     hdr * GC_previous_allocated_hdr;
  51.     
  52.     void GC_print_ptr(p)
  53.     word * p;
  54.     {
  55.         XR_ConsoleMsg("0x%X -> 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X\n",
  56.                    p,      p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
  57.     }
  58. # endif
  59.       
  60. /* Return a previously unallocated hblk header.  No guarantees are made */
  61. /* about its contents.                                                  */
  62. hdr * GC_alloc_hdr()
  63. {
  64.     hdr * result;
  65.  
  66.     if (GC_hdr_fl != HDR_NIL) {
  67.     result = GC_hdr_fl;
  68.     GC_hdr_fl = link(GC_hdr_fl);
  69. #    ifdef DEBUG_SMASH
  70.         GC_previous_allocated_hdr = GC_last_allocated_hdr;
  71.         GC_last_allocated_hdr = result;
  72.         if ((char *)GC_hdr_fl < GC_heapstart
  73.             || (char *)GC_hdr_fl > GC_heaplim + 1000000) {
  74.             if (GC_hdr_fl == 0) {
  75.                 XR_ConsoleMsg("Surprisingly short of hblk headers\n");
  76.             } else {
  77.                 XR_ConsoleMsg("Smashed header free list: 0x%X\n",
  78.                             GC_hdr_fl);
  79.             }
  80.             XR_ConsoleMsg("Last allocated header:\n");
  81.             GC_print_ptr(GC_last_allocated_hdr);
  82.             XR_ConsoleMsg("Previously allocated header:\n");
  83.             GC_print_ptr(GC_previous_allocated_hdr);
  84.         }
  85. #    endif
  86.     } else {
  87.         GC_abort("Out of memory for block headers\n");
  88.     }
  89.     return(result);
  90. }
  91.     
  92.     
  93. /* Release the block at h of size n * HBLKSIZE for use as heap block headers */
  94. /* Must be called sufficiently frequently that we never run out.             */
  95. void GC_use_as_headers(h, n)
  96. word * h;
  97. long n;
  98. {
  99.     register hdr * p;
  100.     register long lim;
  101.  
  102.     if (num_ha + 1 >= MAX_HEADER_AREAS) {
  103.         /* Throw this one away, and hope for the best. */
  104.         GC_iprintf("Too many header areas; discarding one!\n");
  105.         return;
  106.     }
  107.     lim = ((long) h) + n * HBLKSIZE;
  108.     header_areas[num_ha].ha_start = (char *)h;
  109.     header_areas[num_ha].ha_end = (char *)lim;
  110.     num_ha++;
  111.     for (p = (hdr *)h; (long)(p+2) <= lim; p++) {
  112.     set_link(p, p + 1);
  113.     }
  114.     set_link(p, GC_hdr_fl);
  115.     GC_hdr_fl = (hdr *)h;
  116. }
  117.  
  118. # ifdef DIRTY_BITS
  119. /* Unprotect all pages in memory that include only headers.       */
  120. /* This interacts with GCVirtualDirty.c.  It may safely      */
  121. /* be called at any time, PROVIDED we do not care about dirty     */
  122. /* bit information on the headers.  Calling it may improve        */
  123. /* performance.  Note however, that it only affects the           */
  124. /* current virtual processor.                          */
  125. /* This makes sense only if virtual dirty bits are         */
  126. /* implemented with mprotect.                    */
  127. void GC_unprotect_headers()
  128. {
  129.     register int i;
  130.     register long base;
  131.     register long len;
  132.     register int ps = XR_GetPageSize();
  133.     
  134.     for (i = 0; i < num_ha; i++) {
  135.         base = (((long)(header_areas[i].ha_start)) + ps-1) & ~(ps-1);
  136.         len = (((long)(header_areas[i].ha_end)) & ~(ps-1)) - base;
  137.         if (len > 0
  138.             && mprotect(base, len, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
  139.             GC_iprintf("GC_unprotect_headers failed on (0x%X,%d)\n",
  140.                        base, len);
  141.         }
  142.     }
  143. }
  144.  
  145. # endif DIRTY_BITS
  146.  
  147. /* Make sure that a header is available for heap block h */
  148. void GC_get_header(h)
  149. struct hblk * h;
  150. {
  151.     long indx = h - (struct hblk *)GC_heapstart;
  152.     
  153.     if (headers[indx] == HDR_NIL) {
  154.     headers[indx] = GC_alloc_hdr();
  155.     }
  156. }
  157.  
  158. # ifdef UNDEFINED
  159. /* Currently we allocate headers lazily, but we never free them. */
  160. void GC_free_hdr(p)
  161. hdr *p;
  162. {
  163.     set_link(p, GC_hdr_fl);
  164.     GC_hdr_fl = p;
  165. }
  166. # endif UNDEFINED
  167.  
  168. # endif
  169.